package com.winning.devops.demo.base.filter;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;
import com.winning.devops.demo.base.model.LoggerMessage;
import com.winning.devops.demo.base.queue.DisruptorLoggerQueue;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.DateFormat;

/**
 * @author chensj
 * @title
 * @project logback-demo
 * @package com.winning.devops.demo.base.filter
 * @date: 2019-04-09 16:11
 */
@Service
public class ProcessLoggerFilter extends Filter<ILoggingEvent> {

    @Override
    public FilterReply decide(ILoggingEvent event) {
        LoggerMessage loggerMessage = new LoggerMessage(
                event.getMessage(),
                DateFormat.getDateTimeInstance().format(new Date(event.getTimeStamp())),
                event.getThreadName(),
                event.getLoggerName(),
                event.getLevel().levelStr
        );
        try {
            // 触发事件发布机制
            DisruptorLoggerQueue.publishEvent(loggerMessage);
        }catch (NullPointerException e){
            // 处理日志不输出问题
            System.out.print("");
        }

        return FilterReply.ACCEPT;
    }
}
